home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Games / Game Sample Code / ZAM 1.0a13 / GameSource / DirectionTable.c next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  1.2 KB  |  55 lines  |  [TEXT/KAHL]

  1. #include "ZAMProtos.h"
  2. #include <FixMath.h>
  3.  
  4. #define kNumDirectionAngles 32
  5.  
  6. static fixPt    gDirectionTable[kNumDirectionAngles];
  7.  
  8. #define kRadiansInCircle 6.283185307178
  9.  
  10.  
  11. void InitDirectionTable(void)
  12. /*
  13.     this procedure builds a table of fixed point vectors.
  14.     These vectors are indexed by the direction number from 0 to kNumDirectionAngles
  15.     and are evenly spaced around the compass.
  16. */
  17. {
  18.     short i;
  19.     double angle = 0;
  20.     double angleIncrement = kRadiansInCircle / kNumDirectionAngles;
  21.  
  22.     for (i = 0; i < kNumDirectionAngles; i++) {
  23.  
  24.         gDirectionTable[i].h = Frac2Fix( FracSin( X2Fix(angle) ));
  25.         gDirectionTable[i].v = Frac2Fix( -FracCos( X2Fix(angle) ));
  26.  
  27.         angle += angleIncrement;
  28.     }
  29. }
  30.  
  31.  
  32. void DirectionToVelocity(short dir, fixPt *vel)
  33. /*
  34.     grab a vector from the table, indexed by the direction number
  35.     this would be better as a Macro, but hey, it isn't.
  36. */
  37. {
  38.     *vel = gDirectionTable[dir];
  39. }
  40.  
  41.  
  42. short VelocityToDirection(fixPt *vel)
  43. /*
  44.     Get a direction number from a velocity.
  45.     This is a pretty LAME thing to have to do, because it requires a search of the vector table
  46. */
  47. {
  48.     short    i;
  49.     
  50.     for(i = 0; i < kNumDirectionAngles; i++)
  51.         if( (gDirectionTable[i].h == vel->h) && (gDirectionTable[i].v == vel->v))
  52.             return i;
  53.             
  54.     return -1;
  55. }